﻿var bar = document.getElementById('bar');
  var percent = document.getElementById('percent');
  var result = document.getElementById('result');
  var percentValue = "0%";
  
  var HinhNhoInput = document.getElementById('HinhNho');
  var HinhToInput = document.getElementById('HinhTo');
    
  var form = document.getElementById('sanpham_capnhat');
  
  form.addEventListener('submit', function(evt) {
    // Chan khong cho form tao submit
    evt.preventDefault();
    
    // Ajax upload
    var MaSanPham = $('#MaSanPham').val();
    var TenSanPham = $('#TenSanPham').val();
    var GiaGoc = $('#GiaGoc').val();
    var GiaKhuyenMai = $('#GiaKhuyenMai').val();
    var MoTa = CKEDITOR.instances.MoTa.getData();
    var ChiTiet = CKEDITOR.instances.ChiTiet.getData();
    var HuongDan = CKEDITOR.instances.HuongDan.getData();
    var SoLuong = $('#SoLuong').val();
    
    var HinhNho = HinhNhoInput.files[0];
    var HinhTo = HinhToInput.files[0];
    
    var MaDonViTinh = $('#MaDonViTinh').val();
    var MaQuyCach = $('#MaQuyCach').val();
    var MaLoaiSanPham = $('#MaLoaiSanPham').val();
    var MaLoaiSanPhamCap1 = $('#MaLoaiSanPhamCap1').val();
    var MaLoaiSanPhamCap2 = $('#MaLoaiSanPhamCap2').val();
    var MaLoaiSanPhamCap3 = $('#MaLoaiSanPhamCap3').val();
    
    // fd dung de luu gia tri goi len
    var fd = new FormData();
    
    fd.append('MaSanPham', MaSanPham);
    fd.append('TenSanPham', TenSanPham);
    fd.append('GiaGoc', GiaGoc);
    fd.append('GiaKhuyenMai', GiaKhuyenMai);
    fd.append('MoTa', MoTa);
    fd.append('ChiTiet', ChiTiet);
    fd.append('HuongDan', HuongDan);
    fd.append('SoLuong', SoLuong);
    
    fd.append('HinhNho', HinhNho);
    fd.append('HinhTo', HinhTo);
    
    fd.append('MaDonViTinh', MaDonViTinh);
    fd.append('MaQuyCach', MaQuyCach);
    fd.append('MaLoaiSanPham', MaLoaiSanPham);
    fd.append('MaLoaiSanPhamCap1', MaLoaiSanPhamCap1);
    fd.append('MaLoaiSanPhamCap2', MaLoaiSanPhamCap2);
    fd.append('MaLoaiSanPhamCap3', MaLoaiSanPhamCap3);
    
    // xhr dung de goi data bang ajax
    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'sanpham_capnhat_sub_ajax.php', true);
    
    xhr.upload.onprogress = function(e) {
      if (e.lengthComputable) {
        var percentValue = (e.loaded / e.total) * 100 + '%';
        percent.innerHTML  = percentValue;
        bar.setAttribute('style', 'width: ' + percentValue);
      }
    };
    
    xhr.onload = function() {
      if (this.status == 200)
      {
        result.innerHTML = this.response;  
        $('.SanPham').flexReload();      
      };
    };
    
    xhr.send(fd);
    
    
  }, false);